/*
angular.module('moti.controllers.regionslist', ['ng-sortable', 'moti.services.regionsService'])
    .controller('regionsListCtrl', ['$scope', 'RegionsInterface', function($scope, RegionsInterface){

        //获取景区列表
        RegionsInterface.getRegionsList().then(function(data){
            $scope.regions = data;
        });

        //收藏按钮事件
        $scope.regionHot = function(region){
            RegionsInterface.putRegion(region.region_id, {"is_hot" : !region.is_hot}).then(function(data){
                region.is_hot = !region.is_hot;
            });
        };

        //隐藏按钮事件
        $scope.regionHidden = function(region){
            RegionsInterface.putRegion(region.region_id, {"is_hidden" : !region.is_hidden}).then(function(data){
                region.is_hidden = !region.is_hidden;
            });
        };

        //景区排序
        $scope.regionSort = {
            onSort: function(evt){
                var update_pos;
                 if(evt.newIndex == 0){
                     update_pos = evt.models[evt.newIndex + 1].pos - 65535;
                 }else if(evt.newIndex == $scope.regions.length - 1){
                     update_pos = evt.models[evt.newIndex - 1].pos + 65535;
                 }else{
                     update_pos = (evt.models[evt.newIndex - 1].pos + evt.models[evt.newIndex + 1].pos) / 2;
                 }
                RegionsInterface.putRegion(evt.model.region_id, {"pos" : update_pos});
            }
        };

    }]);*/

var data = {regions : null};//存放regions数据
var regionsList = Vue.component('regionsList', {
    template: '#regionsList',
    methods: {
        //收藏按钮事件
        regionHot: function(region){
            vHttp(INTERFACE_API.REGIONDETAIL.replace(":region_id", region.region_id), 'PUT', {"is_hot" : !region.is_hot}, function(response){
                region.is_hot = !region.is_hot;
            });
        },
        //隐藏按钮事件
        regionHidden: function(region){
            vHttp(INTERFACE_API.REGIONDETAIL.replace(":region_id", region.region_id), 'PUT', {"is_hidden" : !region.is_hidden}, function(response){
                region.is_hidden = !region.is_hidden;
            });
        }
    },
    route: {
        data: function(transition){
            //获取景区列表
            vHttp(INTERFACE_API.REGIONS, 'GET', null, function(response){
                data.regions = response;
                transition.next(data);

                //景区排序
                var el = document.getElementById('sortable-list');
                var sortable = new Sortable(el, {
                    onSort: function (evt) {
                        var update_pos;
                        if(evt.newIndex == 0){
                            update_pos = data.regions[evt.newIndex].pos - 65535;
                        }else if(evt.newIndex == response.length - 1){
                            update_pos = data.regions[evt.newIndex].pos + 65535;
                        }else{
                            update_pos = (data.regions[evt.newIndex - 1].pos + data.regions[evt.newIndex].pos) / 2;
                        }
                        vHttp(INTERFACE_API.REGIONDETAIL.replace(":region_id", data.regions[evt.oldIndex].region_id), 'PUT', {"pos" : update_pos}, function(response){
                            //再次获得数据
                            vHttp(INTERFACE_API.REGIONS, 'GET', null, function(response){
                                data.regions = response;
                            });
                        });
                    }
                });
            });
        }
    }
});